#! /bin/bash 

source /etc/profile 

# 可以输入日期；如果未输入日期取昨天的时间 
if [ -n "$1" ] 
then
do_date=$1 
else
do_date=`date -d "-1 day" +%F` 
fi

# 定义要执行的SQL 
sql="
drop table if exists dim.shops_tmp;
create table if not exists dim.shops_tmp as
select
  shopid,
  shopname,
  shoplevel,
  STATUS,
  createtime,
  modifytime, start_dt, end_dt
from dim.dim_trade_shops
where end_dt < '$do_date'
union all
select
  shopid,
  shopname,
  shoplevel,
  STATUS,
  createtime,
  modifytime, start_dt, '9999-12-31' as end_dt
from dim.dim_trade_shops
where start_dt <= '$do_date' and end_dt >= '$do_date';

-- 将临时表中的数据插入商家维表,回滚完成
insert overwrite table dim.dim_trade_shops select * from dim.shops_tmp;
"

hive -e "$sql"
